package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class SettingManager {

    @Reference
    private QuestionService questionService;

    @Reference
    private NotificationService notificationService;

    @Reference
    private BlackListService blackListService;

    //查询通用设置
    public SettingVo findSetting() {
        //1. 获取用户的id和Phone
        User user = UserHolder.get();

        //2. 根据用户id查询陌生人问题
        Question question = questionService.findByUserId(user.getId());

        //3. 根据用户id查询设置
        Notification notification = notificationService.findByUserId(user.getId());

        //4. 组装返回结果
        SettingVo settingVo = new SettingVo();
        BeanUtil.copyProperties(user, settingVo);//id  phone
        if (question != null) {
            BeanUtil.copyProperties(question, settingVo);//strangerQuestion
        }
        if (notification != null) {
            BeanUtil.copyProperties(notification, settingVo);//notification三个
        }
        return settingVo;
    }

    //设置陌生人问题
    public void saveOrUpdateQuestion(String content) {
        //1. 获取用户id
        Long userId = UserHolder.get().getId();

        //2. 根据userId查询陌生人问题
        Question question = questionService.findByUserId(userId);

        if (question == null) {
            //3. 没查到,新增
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        } else {
            //4. 查到,更新
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    //设置通知
    public void saveOrUpdateNotification(Notification notificationParam) {
        //1. 获取用户id
        Long userId = UserHolder.get().getId();

        //2. 根据用户id查询通知设置
        Notification notification = notificationService.findByUserId(userId);

        if (notification == null) {
            //3. 不存在--保存
            notificationParam.setUserId(userId);
            notificationService.save(notificationParam);

        } else {
            //4. 存在--更新
            notification.setPinglunNotification(notificationParam.getPinglunNotification());
            notification.setGonggaoNotification(notificationParam.getGonggaoNotification());
            notification.setLikeNotification(notificationParam.getLikeNotification());
            notificationService.update(notification);
        }
    }

    //黑名单查询
    public PageBeanVo findBlackList(Integer pageNum, Integer pageSize) {
        //1. 获取登录用户id
        Long userId = UserHolder.get().getId();

        //2. 调用service查询
        return blackListService.findBlackList(userId, pageNum, pageSize);
    }

    //移除黑名单用户
    public void deleteBlackUser(Long blackUserId) {
        //1. 获取登录用户id
        Long userId = UserHolder.get().getId();

        //2. 调用service根据两个id删除
        blackListService.delete(userId, blackUserId);
    }
}
